package com.swt.gap.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置类，配置swagger
 *
 * @author sx
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        //添加head参数
        List<Parameter> listParameter = new ArrayList<>();

        List<ResponseMessage> listResponseMessage = new ArrayList<>();
        listResponseMessage.add(new ResponseMessageBuilder().code(404).message("找不到资源").build());
        listResponseMessage.add(new ResponseMessageBuilder().code(401).message("用户未登录").build());
        listResponseMessage.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(listParameter)
                .globalResponseMessage(RequestMethod.GET, listResponseMessage)
                .globalResponseMessage(RequestMethod.POST, listResponseMessage)
                .globalResponseMessage(RequestMethod.PUT, listResponseMessage)
                .globalResponseMessage(RequestMethod.DELETE, listResponseMessage)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//                .apis(RequestHandlerSelectors.basePackage("com.swt.gap.systemmanager.resource"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(setHeaderToken());


    }
    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        pars.add(tokenPar.build());
        return pars;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("GAP Restful API")
                .description("GAP Restful API 文档")
                .contact(new Contact("数为科技", "", ""))
                .version("3.0.0")
                .build();
    }

}
